//
//  SEHospitalServiceButton.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/6.
//

import UIKit

class SEHospitalServiceButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        serviceLabel.frame = CGRect(x: 0, y: center.y - 18, width: bounds.size.width, height: 18)
        detailLabel.frame = CGRect(x: 0, y: center.y + 5, width: bounds.size.width, height: 14)
    }

    private func setUI() {
        addSubview(serviceLabel)
        addSubview(detailLabel)
    }

    public func updateServiceButton(with title: String, detail: String, isEnabled: Bool) {
        serviceLabel.text = title
        detailLabel.text = detail
        self.isEnabled = isEnabled
        if isEnabled {
            serviceLabel.textColor = Asset.textDark.color
            detailLabel.textColor = Asset.main.color
            setBackgroundImage(#imageLiteral(resourceName: "hospital_service_icon"), for: .normal)
            layer.cornerRadius = 6
            layer.borderWidth = 0
        } else {
            setBackgroundImage(nil, for: .normal)
            serviceLabel.textColor = Asset.textGrayLightest.color
            detailLabel.textColor = Asset.textGrayLightest.color
            backgroundColor = Asset.background.color
            layer.cornerRadius = 6
            layer.borderWidth = 1
            layer.borderColor = Asset.border.color.cgColor
        }
    }

    private lazy var serviceLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.medium(16)
        label.textColor = Asset.textDark.color
        return label
    }()

    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.regular(12)
        label.textColor = Asset.textLight.color
        return label
    }()

}
